#include <isa.h>
#include <device/map.h>
#include <device/alarm.h>
#include <utils.h>

static uint64_t *clint_base = NULL;

#define     CLINT_MMIO              0x02000000
#define     CLINT_MTIMECMP_OFFSET   0x4000
#define     CLINT_MTIME_OFFSET      0xBFF8

#define     CLINT_MTIMECMP          clint_base[CLINT_MTIMECMP_OFFSET / sizeof(clint_base[0])]
#define     CLINT_MTIME             clint_base[CLINT_MTIME_OFFSET / sizeof(clint_base[0])]

void clint_update() {
    // 简化设计, 每次执行完一条指令加100
    CLINT_MTIME += 100;
    cpu.csr.mip.MTIP = CLINT_MTIMECMP <= CLINT_MTIME;
}

void init_clint() {
    clint_base = (uint64_t *)new_space(0xC000);
    add_mmio_map("clint", CLINT_MMIO, clint_base, 0xC000, NULL);
    CLINT_MTIME = 0;
    CLINT_MTIMECMP = 500;
}
